<?php

Class SchoolAction extends BaseAction {

	public function index() {
		$this->redirect("schoolIndex");
	}

	/*
	  +----------------------------------------------------------
	 * 学校管理
	  +----------------------------------------------------------
	 */

	public function schoolIndex() {
		//根据搜索框获取内容
		$type = $_GET['type'];
		if ($type == 'school_abbr') {
			$data['school_abbr'] = $_GET['keyword'];
		} else if ($type == 'status') {
			$data['status'] = $_GET['keyword'];
		}

		//列出学校信息
		$School = D('School');
		$school = $School->relation('location')->where($data)->select();

		$this->assign('school', $school);
		$this->display();
	}

	public function schoolAdd() {
		$this->assign('select', 'schoolAdd');
		$this->display('schoolindex');
	}

	//添加学校
	public function schoolAdds() {
		$data['school_name'] = trim($_POST['school_name']);
		$data['school_abbr'] = trim($_POST['school_abbr']);
		$data['status'] = $_POST['status'];
		empty($data['school_name']) && $this->error('学校名称不能空!');
		empty($data['school_abbr']) && $this->error('学校简称不能空!');
		empty($data['status']) && $data['status'] = 0;

		$result = D('School')->data($data)->add();
		if ($result)
			$this->success('修改成功!');
		else
			$this->error('添加失败!');
	}

	public function schoolEdit() {
		$sid = $_GET['school_id'];
		$School = D('School');
		$school = $School->where('school_id =' . $sid)->find();

		$this->assign('school', $school);
		$this->assign('select', 'schoolEdit');
		$this->display('schoolindex');
	}

	//编辑学校信息
	public function schoolEdits() {
		$data['school_id'] = $_POST['school_id'];
		$data['school_name'] = trim($_POST['school_name']);
		$data['school_abbr'] = trim($_POST['school_abbr']);
		$data['status'] = $_POST['status'];
		empty($data['school_name']) && $this->error('学校名称不能空!');
		empty($data['school_abbr']) && $this->error('学校简称不能空!');
		empty($data['status']) && $data['status'] = 1;

		D('School')->data($data)->save();
		$this->success('修改成功!');
	}

	//删除学校(有问题没解决：没有实现关联删除，关联的地点有可能属于多个学校)
	public function schoolDelete() {
		$sid = $_GET['school_id'];
		$result = D('School')->relation('location')->delete($sid);
		if ($result)
			$this->success('删除成功!');
		else
			$this->error('操作失败！');
	}

	/*
	  +----------------------------------------------------------
	 * 地点管理
	  +----------------------------------------------------------
	 */

	public function locationIndex() {
		//根据搜索框获取内容
		$type = $_POST['type'];
		if ($type == 'location_name') {
			$data['location_name'] = array('like', '%' . $_POST['keyword'] . '%');
		} else if ($type == 'status') {
			$data['status'] = $_POST['keyword'];
		}

		//列出地点信息
		$location = D('Location')->relation('school')->where($data)->select();
		//dump($location);
		$this->assign('location', $location);
		$this->display();
	}

	public function locationDelete() {
		$lid = $_GET['location_id'];
		$result = D('Location')->relation('school')->delete($lid);
		if ($result)
			$this->success('删除成功!');
		else
			$this->error('操作失败！');
	}

	public function locationAdd() {
		$school = D('School')->where('status = 1')->select();

		$this->assign('school', $school);
		$this->assign('select', 'locationAdd');
		$this->display('locationindex');
	}

	//添加地点
	public function locationAdds() {
		$Location = D('Location');
		$school_id = $_POST['school_id'];
		$data['location_name'] = trim($_POST['location_name']);
		$data['location_point'] = trim($_POST['location_point']);
		$data['status'] = $_POST['status'];

		empty($data['location_name']) && $this->error('地点名称不能为空!');
		empty($data['status']) ? $data['status'] = 1 : $data['status'];
		foreach ($school_id as $key => $value) {
			$data['school'][$key] = array('school_id' => $value);
		}

		//判断地点是否存在
		$exist = $Location->where('location_name =' . "'" . $data['location_name'] . "'")->find();
		if (!$exist) {
			$result = $Location->relation('school')->add($data);
			if ($result)
				$this->success('添加成功!');
			else
				$this->error('添加失败，请重试');
		}else {
			$this->error('该地点已存在!');
		}
	}

	public function locationEdit() {
		$lid = $_GET['location_id'];
		$location = D('Location')->relation('school')->find($lid);
		foreach ($location['school'] as $key => $value) {
			$id[$key] = $value['school_id'];
		}
		$data['status'] = 1;
		if (!empty($id)) {
			$data['school_id'] = array('not in', $id);
		}
		$school = D('School')->where($data)->select();

		$this->assign('school', $school);
		$this->assign('select', 'locationEdit');
		$this->assign('location', $location);
		$this->display('locationindex');
	}

	//修改地点信息
	public function locationEdits() {
		$school_id = $_POST[school_id];
		$location_id = $_POST['location_id'];
		//修改地点基本信息
		$map['location_id'] = $location_id;

		$map['location_point'] = $_POST['location_point'];
		$map['location_name'] = $_POST['location_name'];
		if (1 == $_POST['status']) {
			$map['status'] = 0;
		} else {
			$map['status'] = 1;
		}
		D('Location')->save($map);

		//修改所属学校
		$School_location = D('School_location');
		$School_location->where('location_id =' . $location_id)->delete();

		$data['location_id'] = $location_id;
		foreach ($school_id as $key => $value) {
			$data['school_id'] = $school_id[$key];
			$School_location->data($data)->add();
		}
		$this->success('修改成功!');
	}

}

?>
